<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\App;
use think\exception\ValidateException;
use think\facade\View;
use think\response\Json;

class Admin extends Base
{
    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Admin';

    //验证器
    protected $validate = 'Admin';

    //关联模型
    protected $hasOne = ['role'];

    public function index()
    {
        return $this->setTable();
    }

    public function setTable()
    {
        $role = \app\common\model\Role::where(['is_del'=> 0, 'type'=>1])->field('id,title')->select();
        $value = [];
        foreach ($role as $v) {
            $value[$v['id']] = $v['title'];
        }
        //构建table
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'admin_name', 'title' => '用户名', 'modify' => 'add', 'format' => 'input'])
            ->addRow(['field' => 'mobile', 'title' => '手机', 'modify' => 'add|edit', 'format' => 'input'])
            ->addRow(['field' => 'role_id', 'title' => '角色', 'templet' => '<span>{{d.role?d.role.title:"暂无"}}</span>'
                , 'format'    => 'select', 'value' => $role, 'modify' => 'add|edit'
            ])
            ->addRow(['field' => 'comment', 'title' => '备注', 'modify' => 'add|edit', 'format' => 'input'])
            ->addRow(['field' => 'password', 'title' => '登录密码', 'modify' => 'add|edit', 'format' => 'password'])
            ->addRow(['field' => 'create_time', 'title' => '添加时间'])
            ->addRow(['field' => 'last_time', 'title' => '最后登录时间'])
            ->addRow(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120, 'unresize' => true])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作'])
            ->clearTable();

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addTopButtons(['add'])
            ->addRightButtons(['edit', 'delete'])
            ->addSwitchTool([['name' => 'status', 'pk' => 'id', 'text' => '正常|锁定']])
            ->setSearch($this->setSearch($value))
            ->addExtraMap(['type'=>1])
            ->fetch('admin/index');
    }

    public function setSearch($value)
    {
        return TableFront::getInstance()
            ->setSearch(['admin_name__mobile__comment', '用户名/手机/备注', 'input', '', '', '', 'like'])
            ->setSearch(['role_id', '角色类型', 'select', $value]);
    }


    public function save($param = [], $model = true): Json
    {
        $param = $this->request->post();
        if (isset($param['password']) && $param['password']) {
            $param['password'] = md5($param['password']);
        }
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }


    public function password()
    {
        if ($this->request->isPost()) {
            $param = $this->request->post();

            try {
                $this->validate($param, 'AdminPassword');
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }

            if (md5($param['old']) != $this->admin->password) {
                return json(['code' => -1, 'msg' => '原密码错误']);
            }

            $this->model::update(['password' => md5($param['password'])], ['id' => $this->admin->id]);

            \app\common\model\Logs::adds('管理员【' . $this->admin->admin_name . '】修改了密码');
            session('admin_id', null);

            return json(['code' => 1, 'msg' => '已修改']);
        }
        return View::fetch();
    }

}
